package com.ztjy.market.controller;


import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.*;
import com.ztjy.market.biz.CustomBiz;
import com.ztjy.sys.biz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/market/customer")
public class CustomController {
    @Autowired
    private CustomBiz ubiz;
    @Autowired
    private AreaBiz abiz;

  //每次执行前会优先 将mid放入request范围
    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 6);
    }



    @MyLog(value = "查询所有客户操作")
    @RequestMapping("/ulist")
    public String all(Model model,Custom custom, @RequestParam(value = "pageno",defaultValue = "1")Integer pageNo){
        Integer pageSize=6;
        PageInfo<Custom> customPage = ubiz.findCustomPage(custom, pageNo, pageSize);
        model.addAttribute("upage",customPage);
        model.addAttribute("custom",custom);
        System.out.println("custom = " + custom);
        return "forward:/market/customer/customerList.html";
    }

    @MyLog(value = "查询客户详情操作")
    @RequestMapping("/find/{id}")
    public String details(@PathVariable("id")Integer customid, HttpServletRequest request){
        Custom byIdCustom = ubiz.findByIdCustom(customid);
        request.setAttribute("custom",byIdCustom);
        return "forward://market/customer/customerView.html";
    }

    @MyLog(value = "状态修改")
    @RequestMapping("/editStatus")
    public String editStatus(Integer customid ,Model model,Integer status ){
        Custom custom = ubiz.findByIdCustom(customid);
        custom.setStatus(status);
        boolean flag = ubiz.updateCustom(custom);
        return "redirect:/market/customer/ulist";
    }

    @MyLog(value = "跳入修改")
    @RequestMapping("/goupdate/{id}")
    public String gouodate(@PathVariable("id")Integer customid, Model model){
        Custom byIdCustom = ubiz.findByIdCustom(customid);
        model.addAttribute("uedit",byIdCustom);
        List<City> cities = abiz.findAllCity();
        model.addAttribute("cities",cities);
        model.addAttribute("custom",byIdCustom);
        return "forward:/market/customer/customerUpdate";
    }

    @MyLog(value = "修改")
    @RequestMapping("editCustom")
    public String doupdate(Custom custom){
        custom.setCreatetime(new Date());
        custom.setDistractime(new Date());
        boolean flag = ubiz.updateCustom(custom);
        if(flag){
            return "redirect:/market/customer/ulist";
        }else{
            return "forward:/market/customer/customerUpdate.html";
        }
    }
    @MyLog(value = "添加方法")
    @RequestMapping("/addCustom")
    public String add(Custom custom){
        boolean flag = ubiz.addCustom(custom);
        if(flag){
            return "redirect:/market/customer/ulist";
        }else{
            return "forward:/market/customer/customerAdd.html";
        }
    }
}